<?
class AdvHomeBottomNewsHot extends Module{
	function AdvHomeBottomNewsHot($row){
		
		Module::Module($row);

		$this->add_form(new AdvHomeBottomNewsHotForm());
	}
}

class AdvHomeBottomNewsHotForm extends Form
{
	function __construct(){
		Form::__construct("AdvHomeBottomNewsHotForm");
	}
	
	function on_submit()
	{
		$url_detail		=	URL::get('url');
		$status			=	URL::get('status','int',0);
		$type			=	URL::get('type','int',1);
		
		$contentConfig	=	$status."\n".$url_detail."\n".$type;
		
		$banner_file	=	DATA_PATH.'adv/AdvHomeBottomNewsHot';
		
		if (isset($_FILES['banner']) && $_FILES['banner']['error']==0){
			$file_ext=strtolower(substr($_FILES['banner']['name'],strlen($_FILES['banner']['name'])-4,4));
			
			if($type==1){//Ảnh
				if(in_array($file_ext,array('.jpg','.gif')))
				{
					if(getimagesize($_FILES['banner']['tmp_name']))
					{	
						if(!move_uploaded_file( $_FILES['banner']['tmp_name'], $banner_file.'.gif'))
						{
							$this->setFormError('banner','Không up load được Banner!');
							return ;
						}
					}
					else
					{
						$this->setFormError('banner','Sai định dạng ảnh!');
						return ;
					}
				}
				else{
					$this->setFormError('banner','Chỉ chấp nhận các định dạng file .jpg , .gif !');
					return ;
				}
			}
			else{
				if(in_array($file_ext,array('.swf')))
				{
					if(!move_uploaded_file( $_FILES['banner']['tmp_name'], $banner_file.'.swf'))
					{
						$this->setFormError('banner','Không up load được Banner!');
						return ;
					}
				}
				else
				{
					$this->setFormError('banner','Flash đại diện chỉ chấp nhận các định dạng file .swf');
					return ;
				}
			}
		}
		
		if(isset(CGlobal::$configs['AdvHomeBottomNewsHot']))
			DB::update('configs',array("conf_val"=>$contentConfig),"conf_key='AdvHomeBottomNewsHot'");
		else
			DB::insert('configs',array("conf_key"=>'AdvHomeBottomNewsHot',"conf_val"=>$contentConfig));
		
		AZLib::get_config(0,1);
		
		Url::redirect_current();//CGlobal::$configs['AdvHomeBottomNewsHot']['conf_val'] = $contentConfig;
	}
	
	function draw()
	{
		global $display;
		$config=@CGlobal::$configs['AdvHomeBottomNewsHot']['conf_val'];
		
		$status			= 0;
		$url_detail		= '';
		$type_display	= 1;
		
		if($config!='')
		{
			$config_arr=explode("\n",$config);
			
			$status				= (int)$config_arr[0];
			$url_detail			= $config_arr[1];
			$type_display		= (int)$config_arr[2];
		}
			
		if($status)
		{//Bật
			if($type_display == 1)
				$display->add('adv_banner',DATA_PATH.'adv/AdvHomeBottomNewsHot.gif');
			else
				$display->add('adv_banner',DATA_PATH.'adv/AdvHomeBottomNewsHot.swf');
				
			$display->add('type_display',	$type_display);
			$display->add('url_detail',		$url_detail);
		}
		else
		{//tắt
			$display->add('adv_banner','');
		}
		
		$display->add('status',$status);
		 
		if(User::have_permit(ADMIN_ADV))
		{
			$display->add('beginform',$this->beginForm(true,'POST',false,false,true));
			$display->add('endform',$this->endForm(true));
			
			$display->add('msg',$this->showFormErrorMessages(1));
			
			$status_arr=array(0=>'Tắt',1=>'Bật');
			$display->add('status_option',AZLib::getOption($status_arr,$status));
			$display->add('is_admin',1);
			
			$display->add('url',$url_detail);
			
			$type_arr	=   array(1=>'Ảnh',2=>'Flash');
			$type		=	URL::get('type','int',$type_display);
			$display->add('type_option',AZLib::getOption($type_arr,$type));  
		}
		else 
		{
			$display->add('adv_config','');
			$display->add('is_admin',0);
		}

		$display->add('adv_width',	200);
		$display->add('adv_height',	250);
		
		$display->output("AdvHomeBottomNewsHot");
	}
}
?>